package org.mapdroid.utils;

import org.mapdroid.R;
import org.mapdroid.data.adt.Parking;
import org.mapdroid.login.LoginActivity;
import org.mapdroid.maps.gmaps.MdMapActivity;
import org.mapdroid.maps.gmaps.overlay.MdMarkerItemizedOverlay;
import org.mapdroid.maps.gmaps.overlay.info.ParkingInfoActivity;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.provider.MediaStore.Audio;

public class MdTimer extends Service{
	public static final int TIMER_NOTIFICATION_ID = 0;
	public static final int FINISH_NOTIFICATION_ID = 1;
	//public static final int TIMER_VALUE = 1800000;
	public static final int TIMER_VALUE = 10000;
	
	private Notification timerNotification, finishNotification;
	private Intent parkingIntent;
	
	@Override
	public void onCreate(){
		super.onCreate();
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		parkingIntent = intent;
		createNotification();
		new CountDownTimer(MdTimer.TIMER_VALUE, 1000){
			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				MdMapActivity.notificationManager.cancel(MdTimer.TIMER_NOTIFICATION_ID);
				
				Intent tmpIntent = parkingIntent;
				Parking tmpParking =  tmpIntent.getParcelableExtra(MdMarkerItemizedOverlay.OBJECT);
				tmpParking.bookingOut(tmpIntent.getStringExtra(LoginActivity.CLIENT_OBJECT));
				tmpIntent.putExtra(MdMarkerItemizedOverlay.OBJECT, tmpParking);
				tmpIntent.setClass(getApplicationContext(), MdMapActivity.class);
				tmpIntent.setAction(Intent.ACTION_SEND);
				PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, tmpIntent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
				
				finishNotification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
				//long[] value = {0,100,200,300};
				//finishNotification.vibrate = value;
				finishNotification.defaults |= Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;
				finishNotification.setLatestEventInfo(getApplicationContext(), getString(R.string.app_name), getString(R.string.book_expired_text), contentIntent);
				MdMapActivity.notificationManager.notify(MdTimer.FINISH_NOTIFICATION_ID, finishNotification);
				
				Intent sendIntent = new Intent(Intent.ACTION_SEND);
				sendIntent.putExtra(MdMarkerItemizedOverlay.OBJECT, tmpParking);
				sendIntent.putExtra(MdMarkerItemizedOverlay.OVERLAY_DATA, tmpIntent.getIntExtra(MdMarkerItemizedOverlay.OVERLAY_DATA, -1));
				sendIntent.putExtra(LoginActivity.CLIENT_OBJECT, tmpIntent.getParcelableExtra(LoginActivity.CLIENT_OBJECT));
				sendBroadcast(sendIntent);
				
				stopSelf();
				return;
			}
	
			@Override
			public void onTick(long millisUntilFinished) {
				//if(!isCanceled){
					int seconds = (int) (millisUntilFinished / 1000) % 60;
					int minutes = (int) (millisUntilFinished / 1000 / 60) % 60;
					// TODO Auto-generated method stub
					setNotification(	getString(R.string.time_remaining_text) + 
										String.format(" %02d", minutes) + ":" + 
										String.format("%02d ", seconds) + 
										getString(R.string.minutes_text) );
				//}
			}
		
		}.start();

		return null;
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
		//notificationManager.cancel(MdTimer.TIMER_NOTIFICATION_ID);
		//cdt.cancel();
	}
	
	public void createNotification(){
		timerNotification = new Notification(android.R.drawable.ic_menu_recent_history, getString(R.string.parking), System.currentTimeMillis());
		finishNotification = new Notification(android.R.drawable.stat_notify_error, getString(R.string.app_name), System.currentTimeMillis());
	}
	
	public void setNotification(String message){
		Intent parkingInfoIntent = parkingIntent;
		parkingInfoIntent.setClass(this, ParkingInfoActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, parkingInfoIntent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
		
		timerNotification.setLatestEventInfo(getApplicationContext(), getString(R.string.parking_notification_label), message, contentIntent);
		MdMapActivity.notificationManager.notify(MdTimer.TIMER_NOTIFICATION_ID, timerNotification);
	}

}
